//
//  DetailViewController.m
//  预购清单
//
//  Created by  on 2022/10/17.
//

#import "DetailViewController.h"
#import "DetailFooter.h"
#import "DetailCell.h"

@interface DetailViewController ()
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSArray *content;
@end

@implementation DetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.title = LS(@"详情页");
    self.array = @[@"类型",@"金额",@"日期",@"支付方式",@"备注"];
    NSString *money = [NSString stringWithFormat:@"%@%0.2f",LS(@"¥"),self.model.money];
    NSString *date = [NSDate transToDate:@"yyyy-MM-dd":self.model.stamp];
    self.content = @[self.model.type.name,money,date,self.model.payType,self.model.descri];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(-10, StuBarHight+5, 40, 34);
    [rightBtn addTarget:self action:@selector(actionForDelete) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = backItem;
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.scrollEnabled = YES;
    [self.tableView registerNib:[DetailCell nib] forCellReuseIdentifier:[DetailCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(16);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(0);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(0);
    self.tableView.c_bottom.c_equal(self.view.c_bottom).coffset(0);
}
- (void)actionForDelete{
    [self alert:LS(@"确认删除？") :LS(@"删除后数据不可恢复") :nil:^{
        [PrepayTool delete:self.model complete:^(BOOL success) {
            [NotifyTool post:notifyPayUpdate :nil];
            [self backBtnAction];
        }];
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:[DetailCell ID]];
    cell.title.text = LS(self.array[indexPath.row]);
    cell.img.hidden = indexPath.row != 3;
    cell.content.hidden = indexPath.row == 3;
    if (indexPath.row == 3) {
        cell.img.image = [UIImage imageNamed:self.content[indexPath.row]];
    }else{
        cell.content.text = LS(self.content[indexPath.row]);
    }
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return (SCREEN_WIDTH - 32) + 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CGFloat h = (SCREEN_WIDTH - 32) + 44;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 32, h)];
    DetailFooter *footer = (DetailFooter*)[DetailFooter loadFromXIB];
    footer.frame = view.bounds;
    footer.img.image = [UIImage imageWithData:self.model.img];
    [view addSubview:footer];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end

